package com.tiny.work.btrace.map;

import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;

import java.lang.reflect.Field;

import static com.sun.btrace.BTraceUtils.*;
import static com.sun.btrace.BTraceUtils.Reflective.get;


@BTrace
public class TraceMapWhenInter {

    @OnMethod(
            clazz = "com.fangdd.customer.service.impl.MapServiceImpl",
            method = "getMapInfo",
            location = @Location(value = Kind.ENTRY)
    )
    public static void gcExecute(@Self Object self,
                                 AnyType[] args) {
        println(Strings.str("Request参数是:"));
        Object houseFilterObject = args[0];
        Field cityIdFiled = field("com.fangdd.customer.entity.business.HouseFilter","cityId");
        Long cityId = (Long) get(cityIdFiled, houseFilterObject);
        String url = strcat(strcat("cityId=", str(cityId)),"&");

        Field geoInRangeFiled = field("com.fangdd.customer.entity.business.HouseFilter","geoInRange");
        Object geoInRange = get(geoInRangeFiled, houseFilterObject);
        url = strcat(strcat(url, strcat("geoInRange=", str(geoInRange))),"&");

        Field geoInBoundingBoxFiled = field("com.fangdd.customer.entity.business.HouseFilter","geoInBoundingBox");
        Object geoInBoundingBox = get(geoInBoundingBoxFiled, houseFilterObject);
        url = strcat(strcat(url, strcat("geoInBoundingBox=", str(geoInBoundingBox))), "&");

        Field sectionIdsFiled = field("com.fangdd.customer.entity.business.HouseFilter","sectionIds");
        Object sectionIds = get(sectionIdsFiled, houseFilterObject);
        url = strcat(strcat(url, strcat("sectionIds=", str(sectionIds))), "&");

        Field blockIdsFiled = field("com.fangdd.customer.entity.business.HouseFilter","blockIds");
        Object blockIds = get(blockIdsFiled, houseFilterObject);
        url = strcat(strcat(url, strcat("blockIds=", str(blockIds))),"&");

        Field areaIdsFiled = field("com.fangdd.customer.entity.business.HouseFilter","areaIds");
        Object areaIds = get(areaIdsFiled, houseFilterObject);
        url = strcat(strcat(url, strcat("areaIds=", str(areaIds))), "&");


        Object houseSources = args[1];
        url = strcat(strcat(url, strcat("geoInBoundingBox=", str(houseSources))), "&");

        Object mapType = args[2];
        url = strcat(strcat(url, strcat("mapType=", str(mapType))), "&");

        Object scale = args[3];
        Field codeFiled = field("com.fangdd.customer.entity.enums.Scale","code");
            int code = (Integer) get(codeFiled, scale);
        url = strcat(strcat(url, strcat("scale=", str(code))),"&");

        Object isClickedByUser = args[4];
        println(strcat("isClickedByUser=", str(isClickedByUser)));

        Object appVersion = args[5];
        println(strcat("appVersion=", str(appVersion)));

        println(strcat("url: ", url));
        println();
    }
    @OnMethod(
            clazz = "com.fangdd.customer.service.impl.MapServiceImpl",
            method = "getMapInfo",
            location = @Location(value = Kind.RETURN)
    )
    public static void gcExecute(@Return Object result){

        println(Strings.str("Response结果是:"));
        Field levelFiled = field("com.fangdd.customer.entity.business.map.MapResponse","level");
        Field pointListFiled = field("com.fangdd.customer.entity.business.map.MapResponse","pointList");
        int level = (Integer) get(levelFiled, result);
        println(BTraceUtils.Strings.strcat("level:", str(level)));
        Object o = get(pointListFiled, result);
        println(str("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"));
        println(o);
        println();
    }
}
